package com.example.graduate.conf.Exception;

import com.alibaba.fastjson.JSONObject;
import com.example.graduate.env.ResponseCodeEnum;
import com.example.graduate.pojo.RechargeSerial;
import com.example.graduate.response.DataResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常处理器
 */
@RestControllerAdvice
@Slf4j
public class ProjectExceptionHandler {
    /**
     * 默认处理器
     * @param e
     * @return
     */
    @ExceptionHandler({Exception.class})
    public DataResponse defaultExceptionHandler(Exception e) {
        log.error("defaultExceptionHandler:"+ JSONObject.toJSONString(e));
        return new DataResponse(ResponseCodeEnum.SYSTEM_ERROR.getCode());
    }

    /**
     * 订单异常处理器
     * @param e
     * @return
     */
    @ExceptionHandler(OrderException.class)
    public DataResponse orderExceptionHandler(OrderException e) {
        log.error("orderExceptionHandler:"+e.getMessage());
        return new DataResponse(e.getMsg());
    }

    /**
     * 充值异常处理器
     * @param e
     * @return
     */
    @ExceptionHandler(RechargeException.class)
    public DataResponse rechargeExceptionHandler(RechargeException e) {
        log.error("rechargeExceptionHandler:"+e.getMessage());
        return new DataResponse(e.getMsg());
    }

    /**
     * 账号异常处理器
     * @param e
     * @return
     */
    @ExceptionHandler(AccountException.class)
    public DataResponse accountExceptionHandler(AccountException e) {
        log.error("accountExceptionHandler:"+e.getMessage());
        return new DataResponse(e.getMsg());
    }
}
